from flask import Blueprint, redirect, render_template, request, session, url_for, jsonify
from sqlalchemy import or_

from module_admin.entity.do.comment import Comment
from module_admin.entity.do.user import SysUser, SysUserRole
from module_admin.utils.http import table_api

student = Blueprint('student', __name__)


@student.route('/student', methods=['GET', "POST"])
def get_student():
    print('123123')
    s_name = session.get('name')
    s_id = session.get('id')
    s_role = session.get('role')
    s_email = session.get('email')
    print(s_email, 22222)
    # 学生主页逻辑
    return render_template('Student_Main.html',
                           title='Student Page',
                           header='Welcome Student',
                           user_name=s_name,
                           user_id=s_id,
                           user_email=s_email,
                           role=s_role)


# 教师相关的路由
@student.route('/get_student_comments', methods=['GET'])
def get_comments_student():
    # 获取前端传递的学生ID
    # student_name = request.args.get('student_name')

    # if not student_name:
    #     return jsonify({"error": "student_id is required"}), 400

    # 根据学生姓名查询所有学生的评论（可能有多个）
    student_comments = Comment.query.filter_by(commenter_type='student').all()
    if not student_comments:
        return {}

    # 格式化返回数据
    result = []
    for comment in student_comments:
        # 查找学生评论或者老师评论
        commenter_name = comment.student_name if comment.commenter_type == 'student' else comment.teacher_name
        comment_data = {
            "commenter_name": commenter_name,
            "content": comment.content
        }
        result.append(comment_data)
    return table_api(data=result)


# 查询子评论接口
@student.route('/get_comments_by_student', methods=['GET'])
def get_comments_by_student():
    # 获取前端传递的学生ID
    student_name = request.args.get('student_name')

    if not student_name:
        return jsonify({"error": "student_id is required"}), 400

    # 根据学生姓名查询所有学生的评论（可能有多个）
    student_comments = Comment.query.filter_by(student_name=student_name).all()
    if not student_comments:
        return {}

    # 根据学生ID找到对应的父评论ID
    # 获取所有父评论ID
    parent_comment_ids = [comment.id for comment in student_comments]

    # 查询所有符合父评论ID的子评论
    comments = Comment.query.filter(Comment.parent_comment_id.in_(parent_comment_ids)).all()

    if not comments:
        return jsonify({"message": "No child comments found for the given students"}), 404

    # 格式化返回数据
    result = []
    for comment in comments:
        # 查找学生评论或者老师评论
        commenter_name = comment.student_name if comment.commenter_type == 'student' else comment.teacher_name
        comment_data = {
            "commenter_name": commenter_name,
            "content": comment.content
        }
        result.append(comment_data)
    return table_api(data=result)


@student.route('/get_comment2_by_student', methods=['GET'])
def get_comment2_by_student():
    # 获取前端传递的学生姓名
    student_name = request.args.get('student_name')

    if not student_name:
        return jsonify({"error": "student_name is required"}), 400

    # 根据学生姓名找到所有评论，获取与老师的评论相关的父评论ID
    # student_comments = Comment.query.filter_by(student_name=student_name).all()
    # 根据学生姓名或教师姓名查询所有评论（符合student_name或者teacher_name）
    student_comments = Comment.query.filter(
        or_(Comment.student_name == student_name, Comment.teacher_name == student_name)).all()

    if not student_comments:
        return jsonify({"message": "No student comments found"}), 404

    result = []
    for student_comment in student_comments:
        # 找到学生评论的父评论（老师的评论）
        teacher_comment = Comment.query.filter_by(parent_comment_id=student_comment.id).first()

        if teacher_comment:
            teacher_comment_data = {
                "commenter_name": teacher_comment.teacher_name,
                "content": teacher_comment.content
            }
        else:
            teacher_comment_data = None  # 如果没有老师评论，设置为None

        student_comment_data = {
            "student_commenter_name": student_comment.student_name,
            "student_comment_content": student_comment.content,
            "teacher_comment": teacher_comment_data
        }
        result.append(student_comment_data)
    return table_api(data=result)
